package com.sq.goodline.service;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class RecToWavService {

	
	/**
	 * 执行单个文件的shell转换
	 * @param file:原rec文件
	 * @param localFilePath：转换后输出的目标文件路径
	 * @return
	 * @throws Exception
	 */
	public boolean doShellConvert(String recFilePath,String targetFilePath) throws Exception {
		String cmd ="/home/kunshi/audiorecord/bin/audioconvert ";
		if(targetFilePath.endsWith(".wav")) {
			cmd = cmd + "-t wav -i " + recFilePath +" -o "+targetFilePath;
		}else if(targetFilePath.endsWith(".mp3")) {
			cmd = cmd + "-t mp3 -b 24 -i " + recFilePath +" -o "+targetFilePath;
		}
		
		log.info("执行shell命令：{}",cmd);
		
		// 执行shell命令
        Process process = Runtime.getRuntime().exec(cmd); 
        // 读取命令的输出
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        StringBuffer sb = new StringBuffer(); 
        String line;
        while ((line = reader.readLine()) != null) {
        	sb.append(line);
        }
        // 等待进程结束
        int exitCode = process.waitFor();
        String result = sb.toString(); 
        log.info(result);
        log.info("执行shell转换完成：Exited with code : " + exitCode);
        if(result.contains("success")) {
        	/**shell执行成功，输出信息为：
        	convert /kunshi1-data/record/20250123/11/20250123113022-2025122_0-13701837383_1-111.8.83.219-120.27.142.124-273.rec success.
        	*/
        	log.info("shell命令执行成功");
        	return true;
        }else {
        	return false;
        }
	}
}
